package com.bawei.room.service.impl;

import com.bawei.common.utils.R;
import com.bawei.room.entity.LiveStreamingEntity;
import com.bawei.room.vo.QueryVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bawei.common.utils.PageUtils;
import com.bawei.common.utils.Query;

import com.bawei.room.dao.LiveStreamingDao;

import com.bawei.room.service.LiveStreamingService;

import java.util.Date;
import java.util.HashMap;



@Service("liveStreamingService")
public class LiveStreamingServiceImpl extends ServiceImpl<LiveStreamingDao, com.bawei.room.entity.LiveStreamingEntity> implements LiveStreamingService {

    @Override
    public PageUtils queryPage(QueryVo vo) {
        //创建查询对象
        QueryWrapper<com.bawei.room.entity.LiveStreamingEntity> wrapper = new QueryWrapper<>();
        //获取key
        String key = vo.getKey();
        //判断key
        //直播间获用户id
        if (StringUtils.isNotEmpty(key)){
            wrapper.like("room_name",key).or().eq("doctor_id",key);
        }
        Integer status = vo.getStatus();
        if (status != null){
            wrapper.eq("room_status",status);
        }
        Object page1 = vo.getPage();
        System.out.println(vo.getPage()+"  "+vo.getLimit());
        HashMap<String, Object> params = new HashMap<>();
        params.put("page",vo.getPage());
        params.put("limit",vo.getLimit());

        IPage<com.bawei.room.entity.LiveStreamingEntity> page = this.page(
                new Query<com.bawei.room.entity.LiveStreamingEntity>().getPage(params),
                wrapper
        );

        //在 config 添加  MyBatisConfig.java 分页配置工具类

        return new PageUtils(page);
    }

    @Override
    public R updateRoomStatus(Integer roomId, Integer status) {
        LiveStreamingEntity liveStreamingEntity = baseMapper.selectById(roomId);
        liveStreamingEntity.setRoomStatus(status);
        //修改直播状态
        //添加结束时间
        liveStreamingEntity.setRoomEndTime(new Date());
        //添加收藏人数
        //点赞人数
        baseMapper.updateById(liveStreamingEntity);
        return R.ok();
    }


}